package org.linuxsogood.boot.ch2;

import org.linuxsogood.boot.ch2.strategy.Predicate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author honway.liu
 * @date 2017/7/5 下午10:03
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class ListFilter {

    public static void main(String[] args) {
        List<Integer> numList = new ArrayList<>();
        numList.add(1);
        numList.add(2);
        numList.add(3);
        numList.add(4);
        numList.add(5);

        List<Integer> integers = filter(numList, num -> num % 2 == 0);
        System.out.println(integers);
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        List<T> result = new ArrayList<>();
        for (T t : list) {
            if (p.test(t)) {
                result.add(t);
            }
        }
        return result;
    }
}
